Pause and Resume Arduino Program using Switch

Arduino is an open source and become famous in very short time, due to its powerful features and simplicity. Arduino comes as boon for inventor, artist etc. One can make their own system without having much knowledge about core electronics and assembly programming.

In this article you will learn how to pause the code and resume using a push button switch. Sometime, we want our code to run only when user do some action like to press switch of get some value of sensor. Few years back while making game arena for manual battel bot, I had used two switches for two team whether they are ready to start match or not. When one team press the switch indicating they are ready, the program will wait for another team to press the switch. When both the team become ready the timer for 3 minutes will start with beep sound. In the tutorial ”” you will learn how to pause the program and resume only when one press the button without using delay function. We cannot use delay function where program have to resume after user interaction.

Pause and Resume Arduino Program using Switch author prototype

Figure 2: Author Prototype of Pause and Resume Arduino Program using Switch

Note: For demo purpose we are using 16×2 alphanumeric LCD.

Components Required Pause and Resume Arduino Program using Switch

Arduino nano x 1

Push Button Switch x 2

10k resistor x 2

16×2 LCD x 1

10K Variable resistor x 1

330E resistor x 1

Circuit Description of Pause and Resume Arduino Program using Switch

The circuit of “” is shown in figure 1, it is built around arduino Nano, two push button switches, an LCD and few other electronic passive components like resistor. LCD is used here only for demo purpose. If you have any confusing about interfacing LCD with arduino then please do watch interfacing video posted in youtube.

Two switches with pulldown resistor is connected to arduino nano analog pin A0 and A1 as shown in circuit diagram. Two pulldown resistors are also used here in order to overcome the transient response of switch. These switches are configured in high level trigger because one end of both switches is connected to +5V power supply.

circuit diagram of Pause and Resume Arduino Program using Switch

Software Code:

Software code is written in arduino programming and compiled using arduino IDE. Let’s see the code. For explanation we are going to split the code in various small section.

Defining the pin of button and LCD used in the project. If you connect the LCD as show in circuit, you do not have to change it but if you modify your connection you also have to change the pin definition.

In setup function we will define the property of button switch i.e. whether we use it as input or output. Similarly, we also begin the LCD.

Loop function run repeatedly, when loop function run, it calls button function and returns to loop function only when button is pressed.

CheckButton() function check whether the button 1 is pressed or not. When button is pressed, the value become high for CheckButtonPress1 and return back to loop function.

 

Complete Software Code

Leave a Comment